package com.applink.sell_proof.database.table;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.Sell;

public class DealDBHelper {
	public static final String TABLE_NAME = "sell_table";
	public static final String ID = "id";
	public static final String CODE = "sell_code";
	public static final String INVOICE_NUMBER = "invoice_number";
	public static final String REWARDCODE = "RewardCode";
	public static final String IMAGE = "Image";
	public static final String DISTRIBUTORINVOICENUMBER = "DistributorInvoiceNumber";
	public static final String DISTRIBUTORID = "DistributorId";
	public static final String INVOICEDATE = "InvoiceDate";
	public static final String STAGE = "stage";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ ID + " integer primary key AUTOINCREMENT," + CODE + " integer ,"
			+ INVOICE_NUMBER + " text ," + REWARDCODE + " text ," + IMAGE
			+ " text ," + DISTRIBUTORINVOICENUMBER + " text ," + DISTRIBUTORID
			+ " integer ," + INVOICEDATE + " integer ," + STAGE + " integer "
			+ ");";

	public static void createDraft(final Sell sell) {
		ContentValues values = new ContentValues();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		values.put(INVOICE_NUMBER, sell.getInvoceNumber());
		values.put(REWARDCODE, sell.getRewardCode());
		values.put(IMAGE, sell.getImage());
		values.put(DISTRIBUTORINVOICENUMBER, sell.getDistributorInvoiceNumber());
		values.put(DISTRIBUTORID, sell.getDistributorId());
		values.put(INVOICEDATE, sell.getInvoiceDate());
		values.put(STAGE, sell.getStage().toInt());
		resolver.insert(EMarketingContentProvider.DEAL_CONTENT_URI, values);
	}

	public static void createSell(final Sell sell) {
		ContentValues values = new ContentValues();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		values.put(CODE, sell.getCode());
		values.put(INVOICE_NUMBER, sell.getInvoceNumber());
		values.put(REWARDCODE, sell.getRewardCode());
		values.put(IMAGE, sell.getImage());
		values.put(DISTRIBUTORINVOICENUMBER, sell.getDistributorInvoiceNumber());
		values.put(DISTRIBUTORID, sell.getDistributorId());
		values.put(INVOICEDATE, sell.getInvoiceDate());
		values.put(STAGE, sell.getStage().toInt());
		resolver.insert(EMarketingContentProvider.DEAL_CONTENT_URI, values);
	}

	public static void updateDraft(final Sell sell) {
		ContentValues values = new ContentValues();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		values.put(INVOICE_NUMBER, sell.getInvoceNumber());
		values.put(REWARDCODE, sell.getRewardCode());
		values.put(IMAGE, sell.getImage());
		values.put(DISTRIBUTORINVOICENUMBER, sell.getDistributorInvoiceNumber());
		values.put(DISTRIBUTORID, sell.getDistributorId());
		values.put(INVOICEDATE, sell.getInvoiceDate());
		values.put(STAGE, sell.getStage().toInt());
		resolver.update(EMarketingContentProvider.DEAL_CONTENT_URI, values, ID
				+ "=?", new String[] { Long.toString(sell.getId()) });

	}

	public static Sell getSellDetail(long id) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.DEAL_CONTENT_URI, null, ID + "=?",
				new String[] { Long.toString(id) }, null);

		if (cursor != null) {
			try {
				if (cursor.moveToFirst()) {
					Sell sell = new Sell();
					sell.setId(id);
					sell.setCode(cursor.getLong(cursor.getColumnIndex(CODE)));
					sell.setInvoceNumber(cursor.getString(cursor
							.getColumnIndex(INVOICE_NUMBER)));
					sell.setRewardCode(cursor.getString(cursor
							.getColumnIndex(REWARDCODE)));
					sell.setImage(cursor.getString(cursor.getColumnIndex(IMAGE)));
					sell.setDistributorInvoiceNumber(cursor.getString(cursor
							.getColumnIndex(DISTRIBUTORINVOICENUMBER)));
					sell.setDistributorId(cursor.getLong(cursor
							.getColumnIndex(DISTRIBUTORID)));
					sell.setInvoiceDate(cursor.getLong(cursor
							.getColumnIndex(INVOICEDATE)));
					sell.setStage(Sell.STAGE.getStage(cursor.getInt(cursor
							.getColumnIndex(STAGE))));
					return sell;
				}
			} finally {
				cursor.close();
			}
		}
		return null;
	}

	public static void updateDealStage(final long code, final int stage) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues values = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				values.put(STAGE, stage);
				resolver.update(EMarketingContentProvider.DEAL_CONTENT_URI,
						values, CODE + "=?",
						new String[] { Long.toString(code) });
			}

		}.start();
	}

	public static void deleteDeal(long Id) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.delete(EMarketingContentProvider.DEAL_CONTENT_URI, ID + "=?",
				new String[] { Long.toString(Id) });
	}

}
